/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xuidemo.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Text;

import com.xuexiang.xui_lib.component.textview.MarqueeTextView;
import com.xuexiang.xui_lib.component.textview.marqueen.ComplexItemEntity;
import com.xuexiang.xui_lib.component.textview.marqueen.ComplexViewMF;
import com.xuexiang.xui_lib.component.textview.marqueen.DisplayEntity;
import com.xuexiang.xui_lib.component.textview.marqueen.MarqueeFactory;
import com.xuexiang.xui_lib.component.textview.marqueen.MarqueeView;
import com.xuexiang.xui_lib.component.textview.marqueen.SimpleNoticeMF;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.util.XToastUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 文字滚动Demo
 *
 * @since 2021-04-07
 */
public class MarqueenAbilitySlice extends AbilitySlice {
    private MarqueeView marqueeView1, marqueeView2, marqueeView3, marqueeView4, marqueeView5;
    private MarqueeTextView mTvMarquee;
    private List<String> flipsDrawText = Arrays.asList("《赋得古原草送别》",
        "离离原上草，一岁一枯荣。", "野火烧不尽，春风吹又生。", "远芳侵古道，晴翠接荒城。", "又送王孙去，萋萋满别情。");

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_marqueen);

        initView();
    }

    /**
     * 初始化
     */
    public void initView() {
        findComponentById(ResourceTable.Id_marqueeview_backImage).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminate();
            }
        });
        marqueeView1 = (MarqueeView) findComponentById(ResourceTable.Id_marqueeView1);
        marqueeView2 = (MarqueeView) findComponentById(ResourceTable.Id_marqueeView2);
        marqueeView3 = (MarqueeView) findComponentById(ResourceTable.Id_marqueeView3);
        marqueeView4 = (MarqueeView) findComponentById(ResourceTable.Id_marqueeView4);
        marqueeView5 = (MarqueeView) findComponentById(ResourceTable.Id_marqueeView5);

        init1();
        init2();
        init3();
        init4();
        init5();
        init6();
    }

    private void init1() {
        MarqueeFactory<Text, String> marqueeFactory1 = new SimpleNoticeMF(getContext());
        marqueeFactory1.setOnItemClickListener((view, holder) -> XToastUtils.toast(holder.getData()));
        marqueeFactory1.setData(flipsDrawText);

        marqueeView1.setMarqueeFactory(marqueeFactory1);
        marqueeView1.startFlipping();
    }

    private void init2() {
        MarqueeFactory<Text, String> marqueeFactory2 = new SimpleNoticeMF(getContext());
        marqueeFactory2.setOnItemClickListener((view, holder) -> XToastUtils.toast(holder.getData()));
        marqueeFactory2.setData(flipsDrawText);

        AnimatorProperty mAnimIn = new AnimatorProperty();
        mAnimIn.moveFromX(1080).moveToX(0).alphaFrom(0).alpha(1).setDuration(500);

        AnimatorProperty mAnimOut = new AnimatorProperty();
        mAnimOut.moveFromX(0).moveToX(-1080).alphaFrom(1).alpha(0).setDuration(500);
        marqueeView2.setAnimInAndOut(mAnimIn, mAnimOut);
        marqueeView2.setMarqueeFactory(marqueeFactory2);
        marqueeView2.setAnimDuration(15000);
        marqueeView2.setInterval(16000);
        marqueeView2.startFlipping();
    }

    private void init3() {
        MarqueeFactory<Text, String> marqueeFactory3 = new SimpleNoticeMF(getContext());
        marqueeFactory3.setOnItemClickListener((view, holder) -> XToastUtils.toast(holder.getData()));
        marqueeFactory3.setData(flipsDrawText);
        marqueeView3.setMarqueeFactory(marqueeFactory3);
        AnimatorProperty mAnimIn = new AnimatorProperty();
        mAnimIn.moveFromX(-1080).moveToX(0).alphaFrom(0).alpha(1).setDuration(500);

        AnimatorProperty mAnimOut = new AnimatorProperty();
        mAnimOut.moveFromX(0).moveToX(1080).alphaFrom(1).alpha(0).setDuration(500);

        marqueeView3.setAnimInAndOut(mAnimIn, mAnimOut);
        marqueeView3.setAnimDuration(8000);
        marqueeView3.setInterval(8500);
        marqueeView3.startFlipping();
    }

    private void init4() {
        MarqueeFactory<Text, String> marqueeFactory4 = new SimpleNoticeMF(getContext());
        marqueeFactory4.setOnItemClickListener((view, holder) -> XToastUtils.toast(holder.getData()));
        marqueeFactory4.setData(flipsDrawText);

        AnimatorProperty mAnimIn = new AnimatorProperty();
        mAnimIn.moveFromY(-marqueeView3.getHeight()).moveToY(0).alphaFrom(0).alpha(1).setDuration(500);

        AnimatorProperty mAnimOut = new AnimatorProperty();
        mAnimOut.moveFromY(0).moveToY(marqueeView3.getHeight()).alphaFrom(1).alpha(0).setDuration(500);

        marqueeView4.setAnimInAndOut(mAnimIn, mAnimOut);
        marqueeView4.setMarqueeFactory(marqueeFactory4);
        marqueeView4.startFlipping();
    }

    private void init5() {
        List<ComplexItemEntity> complexDatas = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            complexDatas.add(new ComplexItemEntity("标题 " + i, "副标题 " + i, "时间 " + i));
        }
        MarqueeFactory<DependentLayout, ComplexItemEntity> marqueeFactory5 = new ComplexViewMF(getContext());
        marqueeFactory5.setOnItemClickListener((view, holder) -> XToastUtils.toast(holder.getData().toString()));
        marqueeFactory5.setData(complexDatas);

        AnimatorProperty mAnimIn = new AnimatorProperty();
        mAnimIn.moveFromY(-marqueeView5.getHeight()).moveToY(0).alphaFrom(0).alpha(1).setDuration(500);

        AnimatorProperty mAnimOut = new AnimatorProperty();
        mAnimOut.moveFromY(0).moveToY(marqueeView5.getHeight()).alphaFrom(1).alpha(0).setDuration(500);

        marqueeView5.setAnimInAndOut(mAnimIn, mAnimOut);
        marqueeView5.setMarqueeFactory(marqueeFactory5);
        marqueeView5.startFlipping();
    }

    private void init6() {
        mTvMarquee = (MarqueeTextView) findComponentById(ResourceTable.Id_tv_marquee);

        mTvMarquee.setOnMarqueeListener(new MarqueeTextView.OnMarqueeListener() {

            @Override
            public DisplayEntity onStartMarquee(DisplayEntity displayMsg, int index) {
                if ("离离原上草，一岁一枯荣。".equals(displayMsg.toString())) {
                    return null;
                } else {
                    XToastUtils.toast("开始滚动：" + displayMsg.toString());
                    return displayMsg;
                }
            }

            @Override
            public List<DisplayEntity> onMarqueeFinished(List<DisplayEntity> displayDatas) {
                XToastUtils.toast("一轮滚动完毕！");
                return displayDatas;
            }
        });
        mTvMarquee.startSimpleRoll(flipsDrawText);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mTvMarquee.clear();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
